﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Deflection
{
    class WaveFactory
    {
        private static WaveFactory instance = null;


        public List<Wave> Waves { get; set; }
        public int CurrentWaveIndex { get; set; }

        public List<Wave> GetWaves()
        {
            return Waves;
        }
        public int waveNumber = 0;
        public float spawnInterval = 2;
        public static WaveFactory Instance()
        {
            if (instance == null)
            {
                instance = new WaveFactory();
            }
            return instance;
        }

        private WaveFactory()
        {
            Waves = new List<Wave>();
            CurrentWaveIndex = 0;
            // test wave 1
            // 8 enemies in zigzag formation
            // spawning left side of screen at 1 sec intervals
            /*Path path = new Path();
            
            path = new Path();
            path.Nodes = Formations.ZigZag;
            path.position = new Vector2(840, 360);
            path.scale = 1.0f;

            Wave testWave = new Wave(path, 8, Enemy.EnemyType.SquareEnemy, 1000.0f, EnemyFactory.ScreenSide.Left);
            Waves.Add(testWave);*/         

            // test wave 2
            // 20 enemies in box formation
            // spawning all sides of screen at 15 sec intervals
            //Path path2 = new Path();

            //path2 = new Path();
            //path2.Nodes = Paths.Box;
            //path2.position = new Vector2(840, 360);
            //path2.scale = 1.0f;

            //Wave testWave2 = new Wave(path2, 4, Enemy.EnemyType.SquareEnemy, 15000.0f, EnemyFactory.ScreenSide.All);
            //waves.Add(testWave2);


        }

        public void Update(GameTime gameTime)
        {
            Waves.ForEach(delegate(Wave w)
            {
                if (!w.WaveComplete)
                {
                    w.Update(gameTime);
                }
            });
            Waves.RemoveAll(FindCompletedWaves);
            if (Waves.Count == 0)
            {
                CurrentWaveIndex++;
                CreateNewWave();
            }
        }
        private bool FindCompletedWaves(Wave w)
        {
            if (w.WaveComplete)
            {
                return true;
            }
            return false;
        }
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Vector2 textSize = GameParameters.GAME_FONT.MeasureString("Wave " + CurrentWaveIndex.ToString());
            spriteBatch.DrawString(GameParameters.GAME_FONT, "Wave " + CurrentWaveIndex.ToString(), new Vector2(GameParameters.SCREEN_SIZE.X / 2, 50.0f), Color.Black, 0f, textSize, 0.45f, SpriteEffects.None, 0f);
        }

        public void CreateNewWave()
        {
            Wave wave = new Wave(CurrentWaveIndex, spawnInterval);
            Waves.Add(wave);
        }
    }
}
